//
//  BGInitArea.m
//  bigMall
//
//  Created by LTOVE on 16/8/23.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGInitArea.h"
#define acherNameKey @"areaNameKey"
#define acherIdstrKey @"areaIdstrKey"
@implementation BGInitArea


+ (id)idstrWithIndexArray:(NSArray *)indexArray
{
    NSMutableArray *idStrArray = [NSMutableArray array];
    NSArray *array = [BGInitArea searchArrayWithDicKey:@"id" acherkey:acherIdstrKey];
    NSArray *oneArray = [array firstObject];
    NSArray *twoArray = array[1];
    NSArray *thressArray = array[2];
    NSInteger oneIndex = [[indexArray firstObject] integerValue];
    NSInteger twoIndex = [indexArray[1] integerValue];
    NSInteger thressIndex = [[indexArray lastObject] integerValue];
    [idStrArray addObject:oneArray[oneIndex]];
    [idStrArray addObject:twoArray[oneIndex][twoIndex]];
    [idStrArray addObject:thressArray[oneIndex][twoIndex][thressIndex]];
//    for (int i = 0; i < indexArray.count; i ++) {
//        
//        NSString *index = indexArray[i];
//        if (array.count - 1 >= i) {
//            NSArray *oneArray = array[i];
//        }
//        [idStrArray addObject:array[[index intValue]]];
//        a
//    }
    return idStrArray;
}

+ (NSArray *)initAreaArray
{
  return  [BGInitArea searchArrayWithDicKey:@"name" acherkey:acherNameKey];
}

+ (id)searchArrayWithDicKey:(NSString *)dicKey
                   acherkey:(NSString *)achkey
{
    NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:achkey];
    if (array) {
        return array;
    }else{
        NSString *path = [[NSBundle mainBundle]pathForResource:@"area.plist" ofType:nil];
        NSArray *soureArray = [NSArray arrayWithContentsOfFile:path];
        soureArray = [soureArray firstObject];
        NSMutableArray *provienceArray = [NSMutableArray array];
        NSMutableArray *cityArray = [NSMutableArray array];
        NSMutableArray *contryArray = [NSMutableArray array];
        for (int i = 0; i < soureArray.count; i++) {
            NSDictionary *dic = soureArray[i];
            NSString *provience = dic[dicKey];
            NSArray *secArray = dic[@"child"];
            NSMutableArray *twoArray = [NSMutableArray array];
            NSMutableArray *threeArray = [NSMutableArray array];
            [provienceArray addObject:provience];
            [cityArray addObject:twoArray];
            [contryArray addObject:threeArray];
            for (int j = 0; j < secArray.count; j++) {
                NSDictionary *cityDic = secArray[j];
                NSString *city = cityDic[dicKey];
                [twoArray addObject:city];
                NSArray *threSourArray = cityDic[@"child"];
                NSMutableArray *contryArrays = [NSMutableArray array];
                [threeArray addObject:contryArrays];
                for (NSDictionary *sourThreeDic in threSourArray) {
                    NSString *contryStr = sourThreeDic[dicKey];
                    [contryArrays addObject:contryStr];
                }
            }
        }
        
        NSMutableArray *returnArray = [NSMutableArray arrayWithObjects:provienceArray,cityArray,contryArray, nil];
        [[NSUserDefaults standardUserDefaults]setObject:returnArray forKey:achkey];
        return returnArray;
    }

}

@end
